package com.nannybear.Controller;

import android.view.View;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.TextView;

import com.nannybear.R;
import com.nannybear.View.TimelineView;

/**
 * \brief Controller of babyState Screen
 * \author Audrey LOISEAU
 * \date 22-03-2015
 */

/********************************* COPYRIGHT *********************************
 * Copyright (c) 2015, Audrey LOISEAU and Benjamin LANCINOT.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *************************************************************************************/

public class TimelineController {
    /********** DECLARATIONS **********/
    // Classes
    private ViewPuppeteer viewPuppeteer;

    // Variables
    private static final long HourToMs = 3600000;
    private static final long MinToMs = 60000;

    /************ METHODS ************/
    /**
     * \brief Constructor
     * \param viewPuppeteer The viewPuppeteer class
     */
    public TimelineController(ViewPuppeteer viewPuppeteer){
        this.viewPuppeteer = viewPuppeteer;
    }

    /**
     * \brief Initialization of the interface (creation and listener)
     */
    public void initInterface(){
        long firstRadioButtonHValue = (long)(this.viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hour_timeline_1",0) );
        long firstRadioButtonMValue = (long)(this.viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("min_timeline_1",0) );
        long secondRadioButtonHValue = (long)(this.viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("hour_timeline_2",0));
        long secondRadioButtonMValue = (long)(this.viewPuppeteer.getmActivity().getYoda().retrievesSharedPreferencesLong("min_timeline_2",0));
        String valueOfRadioButton1;
        String valueOfRadioButton2;

        final long firstRadioValue;
        final long secondRadioValue;

        if (firstRadioButtonHValue ==0 && firstRadioButtonMValue ==0){
            firstRadioValue= 2* HourToMs;
            valueOfRadioButton1 = "Last 2h";
        }else{
            valueOfRadioButton1 ="Last ";
            firstRadioValue= (firstRadioButtonHValue* HourToMs) + (firstRadioButtonMValue* MinToMs);
            if(firstRadioButtonHValue != 0) {
                valueOfRadioButton1 += String.valueOf(firstRadioButtonHValue)+"h";
            }
            if(firstRadioButtonMValue != 0) {
                valueOfRadioButton1 += String.valueOf(firstRadioButtonMValue)+"min";
            }
        }

        if (secondRadioButtonHValue ==0 && secondRadioButtonMValue ==0){
            secondRadioValue= 4* HourToMs;
            valueOfRadioButton2 = "Last 4h";
        }else{
            valueOfRadioButton2 ="Last ";
            secondRadioValue= (secondRadioButtonHValue* HourToMs) + (secondRadioButtonMValue* MinToMs);
            if(secondRadioButtonHValue != 0) {
                valueOfRadioButton2 += String.valueOf(secondRadioButtonHValue)+"h";
            }
            if(secondRadioButtonMValue != 0) {
                valueOfRadioButton2 += String.valueOf(secondRadioButtonMValue)+"min";
            }
        }

        TimelineView timelineView = (TimelineView) viewPuppeteer.getmActivity().findViewById(R.id.seekBar0);
        viewPuppeteer.getmActivity().getBabyStateScreen().setTimelineView(timelineView);

        final SeekBar mSeekbar = (SeekBar) viewPuppeteer.getmActivity().findViewById(R.id.bbstate_seekBar);

        mSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            int progressVal;
            @Override
            public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) {
                viewPuppeteer.getmActivity().getBabyStateScreen().displayValueOnProgressBar(progressValue);
                viewPuppeteer.getmActivity().getBabyStateScreen().displayValueOnProgressBar(progressValue);
                progressVal =progressValue;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                viewPuppeteer.getmActivity().getTimeline().customTime(progressVal);
                viewPuppeteer.getmActivity().getBabyStateScreen().setRadioButtonSelected(-1);
            }
        });

        // Initialization of radioButton (settingsScreen)
        final RadioButton radioButton_overview_1 = (RadioButton) viewPuppeteer.getmActivity().findViewById(R.id.radioButton_overview_1);

            RadioButton radioButton_2 = (RadioButton) viewPuppeteer.getmActivity().findViewById(R.id.radioButton_2);
            radioButton_2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewPuppeteer.getmActivity().getBabyStateScreen().customButtonAvailable(false);
                    viewPuppeteer.getmActivity().getTimeline().setTimeOfRadioButton((int)firstRadioValue);
                    viewPuppeteer.getmActivity().getBabyStateScreen().setRadioButtonSelected(1);
                    viewPuppeteer.getmActivity().getBabyStateScreen().displayTimeStep(firstRadioValue);
                }
            });

            radioButton_2.setText(valueOfRadioButton1);
            viewPuppeteer.getmActivity().getBabyStateScreen().setRadioButton_2(radioButton_2);
            viewPuppeteer.getmActivity().getBabyStateScreen().setFirstRadioValue(firstRadioValue);
            viewPuppeteer.getmActivity().getBabyStateScreen().setFirstButtonIsVisible(false);

            RadioButton radioButton_3 = (RadioButton) viewPuppeteer.getmActivity().findViewById(R.id.radioButton_3);
            radioButton_3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewPuppeteer.getmActivity().getBabyStateScreen().customButtonAvailable(false);
                    viewPuppeteer.getmActivity().getTimeline().setTimeOfRadioButton((int)secondRadioValue);
                    viewPuppeteer.getmActivity().getBabyStateScreen().setRadioButtonSelected(2);
                    viewPuppeteer.getmActivity().getBabyStateScreen().displayTimeStep(secondRadioValue);
                }
            });
            radioButton_3.setText(valueOfRadioButton2);
            viewPuppeteer.getmActivity().getBabyStateScreen().setRadioButton_3(radioButton_3);
            viewPuppeteer.getmActivity().getBabyStateScreen().setSecondRadioValue(secondRadioValue);
            viewPuppeteer.getmActivity().getBabyStateScreen().setSecondButtonIsVisible(false);

        viewPuppeteer.getmActivity().getBabyStateScreen().setSeekbar(mSeekbar);
        RadioButton radioButton_4 = (RadioButton) viewPuppeteer.getmActivity().findViewById(R.id.radioButton_4);

        radioButton_overview_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getBabyStateScreen().customButtonAvailable(false);
                viewPuppeteer.getmActivity().getTimeline().setTimeOfRadioButton(0);
                viewPuppeteer.getmActivity().getBabyStateScreen().setRadioButtonSelected(0);
            }
        });
        radioButton_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getBabyStateScreen().customButtonAvailable(true);
                viewPuppeteer.getmActivity().getTimeline().customTime(50);
                viewPuppeteer.getmActivity().getBabyStateScreen().setRadioButtonSelected(-1);
                viewPuppeteer.getmActivity().getBabyStateScreen().displayValueOnProgressBar(50);
                mSeekbar.setProgress(50);
            }
        });

        viewPuppeteer.getmActivity().getBabyStateScreen().setRadioButton_4(radioButton_4);
        viewPuppeteer.getmActivity().getBabyStateScreen().setProgressBarValue((TextView)viewPuppeteer.getmActivity().findViewById(R.id.progressValue));
        viewPuppeteer.getmActivity().getBabyStateScreen().setRadioButtonGroup(viewPuppeteer.getmActivity().findViewById(R.id.radio_button_group));

        //Overview at the opening
        radioButton_overview_1.setChecked(true);
        viewPuppeteer.getmActivity().getBabyStateScreen().setRadioButton_overview_1(radioButton_overview_1);
        viewPuppeteer.getmActivity().getBabyStateScreen().customButtonAvailable(false);
        viewPuppeteer.getmActivity().getTimeline().setTimeOfRadioButton(0);
        viewPuppeteer.getmActivity().getBabyStateScreen().setRadioButtonSelected(0);

        //display radio button 1 and/or 2 if its necessary
        if (this.viewPuppeteer.getmActivity().getBabyStateScreen().getTotalTime() > firstRadioValue) {
            this.viewPuppeteer.getmActivity().getBabyStateScreen().getRadioButton_2().setVisibility(View.VISIBLE);
            this.viewPuppeteer.getmActivity().getBabyStateScreen().setFirstButtonIsVisible(true);
        }
        if (this.viewPuppeteer.getmActivity().getBabyStateScreen().getTotalTime() > secondRadioValue) {
            this.viewPuppeteer.getmActivity().getBabyStateScreen().getRadioButton_3().setVisibility(View.VISIBLE);
            this.viewPuppeteer.getmActivity().getBabyStateScreen().setSecondButtonIsVisible(true);
        }
    }
}
